package com.phonetools.commonutils.regex

import java.util.regex.Pattern

/**
 * 验证手机号码
 *
 * @return
 */
fun isPhoneNumber(input: String): Boolean {
    return try {
        val regex = Pattern.compile("^(1)\\d{10}$")
        val matcher = regex.matcher(input)
        matcher.matches()
    } catch (e: Exception) {
        false
    }
}

/**
 * 固定电话号码
 *
 * @return
 */
fun isTelephoneNumber(input: String): Boolean {
    val p = Pattern.compile("^[0][0-9]{2,3}-?[0-9]{5,10}$")  // 验证带区号的,中间有"-"
    return p.matcher(input).matches()
}

/**
 * 固定电话号码无区号
 *
 * @return
 */
fun isTelephoneNumberWithoutArea(input: String): Boolean {
    val p = Pattern.compile("^[1-9]{1}[0-9]{5,8}$")         // 验证没有区号的
    return p.matcher(input).matches()
}

/**
 * 验证邮箱
 *
 * @return
 */
fun isEmail(input: String): Boolean {
    return try {
        val check =
            "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"
        val regex = Pattern.compile(check)
        val matcher = regex.matcher(input)
        matcher.matches()
    } catch (e: Exception) {
        false
    }
}

